package cn.hs.ph.servlet;
import cn.hs.ph.dao.GoodsDao;
import cn.hs.ph.dao.impl.GoodsDaoImpl;
import cn.hs.ph.entity.Goods;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.编码
        //2.接收数据
        String action=req.getParameter("action");
        GoodsDao goodsDao=new GoodsDaoImpl();
        //3.处理action根据其来进行判断
        if (action.equals("selectAll")){
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String goodsName = req.getParameter("goodsName");
            String goodsShopName =req.getParameter("goodsShopName");

            Map<String,String> map=new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("goodsName",goodsName);
            map.put("goodsShopName",goodsShopName);
            List<Goods> list=goodsDao.selectAll(map);

            int total=goodsDao.selectCount(map);
            Map resultMap=new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            //4.做出响应
            Result<Goods> result=Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }
        else if (action.equals("delete")){
            int goodsId=Integer.parseInt(req.getParameter("goodsId"));
            int num = goodsDao.delete(goodsId);
            Result<Goods> result=Result.success("删除成功");
            resp.getWriter().write(JSON.toJSONString(result));
        }
        else if (action.equals("update")){

        }else if (action.equals("modify")){
            int goodsId= Integer.parseInt(req.getParameter("goodsId"));
            int goodsStatus=Integer.parseInt(req.getParameter("goodsStatus"));
            Goods goods=new Goods();
            goods.setGoodsId(goodsId);
            goods.setGoodsStatus(goodsStatus);
            int num = goodsDao.modify(goods);
            if (num!=0){
                Result<Goods> result=Result.success("修改成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
